package org.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo.entity.InformationHint;
import org.example.demo.mapper.InformationHintMapper;
import org.example.demo.service.InformationHintService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * 信息提示服务实现类
 */
@Service
public class InformationHintServiceImpl extends ServiceImpl<InformationHintMapper, InformationHint>
        implements InformationHintService {

    @Override
    public Page<InformationHint> selectPage(InformationHint informationHint, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<InformationHint> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (informationHint != null) {
            if (StringUtils.hasText(informationHint.getTitle())) {
                queryWrapper.like(InformationHint::getTitle, informationHint.getTitle());
            }
            if (StringUtils.hasText(informationHint.getContent())) {
                queryWrapper.like(InformationHint::getContent, informationHint.getContent());
            }
            if (informationHint.getUserId() != null) {
                queryWrapper.eq(InformationHint::getUserId, informationHint.getUserId());
            }
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc(InformationHint::getCreateTime);

        return page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    public Page<InformationHint> getByUserId(Long userId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<InformationHint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InformationHint::getUserId, userId)
                .orderByDesc(InformationHint::getCreateTime);

        return page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    public boolean save(InformationHint entity) {
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        return super.save(entity);
    }

    @Override
    public boolean updateById(InformationHint entity) {
        entity.setUpdateTime(LocalDateTime.now());
        return super.updateById(entity);
    }
}